home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / mac / sparkle / spark202.hqx / Docs / Technical notes < prev   
Encoding:
Text File  |  1994-05-17  |  28.7 KB  |  583 lines

  1. Contents:
  2. ABOUT MPEG
  3. ABOUT CONVERSION TO QUICKTIME
  4. ABOUT QT VM
  5. MISC SIZE AND TIMING NOTES
  6. ABOUT THE THREAD USAGE
  7. ABOUT THE RESOURCES.
  8. TIMING FOR WAITNEXTEVENT() AND FRIENDS
  9. ACCURATE TIMINGS FOR SPARKLE.
  10.  
  11.  
  12. This file contains various pieces of informationa about MPEG and Sparkle. 
  13. Parts of it are notes I take for myself as I alter and test the program.
  14. I included them here because I thought some other mac programmers out there
  15. might be interested in such things. Read through what you care about and 
  16. understand and ignore the rest.
  17.  
  18.  
  19. -------------------------------------------------------------------------------
  20. ABOUT MPEG
  21.  
  22. MPEG is an international standard for video compression. It compresses 
  23. frames at two levels. Firstly frames are compressed internally, and 
  24. secondly frame differences are compressed rather than transmitting full 
  25. frames. 
  26. To understand MPEG one should first understand JPEG. MPEG uses the same 
  27. ideas as JPEG for much of its compression, and suffers from the same 
  28. limitations. 
  29. JPEG compression begins by changing an image's color space from RGB 
  30. planes to YUV planes, where Y is the luminance (brightness of the image) 
  31. and U and V store color information about the image. Half the color 
  32. resolution in the horizontal and vertical planes is dropped, because the 
  33. eye is less sensitive to color than to brightness. These YUV planes are 
  34. then divided into 8 by 8 blocks of pixels. The 64 coefficients in this 8 
  35. by 8 block are fourier transformed concentrating the energy of the image 
  36. in a few coefficients at low frequencies. The high frequency terms of the 
  37. fourier transform can be discarded as the eye is not sensitive to them. 
  38. The resultant fourier transform coefficients are then encoded using a 
  39. variable length coding scheme (basically Huffman coding) so that 
  40. frequently occuring patterns of coefficients are transmitted in few bits 
  41. and rare patterns transmitted in many bits.
  42.  
  43. MPEG goes beyond this by adding support for inter-frame compression. This 
  44. compression works by realizing that most video consists of foreground 
  45. objects moving over a largely static background. Thus rather than 
  46. transmit the foreground and background pictures over and over again, what 
  47. is transmitted is the motion of the foreground objects. Note that this is 
  48. different from the way QuickTime does interframe compression. What 
  49. QuickTime does is just to subtract the two images from each other and 
  50. compress the resultant image (which is hopefully largely blank space.)
  51. The MPEG scheme gives much better compression, but is much harder to 
  52. program. It is essentially a pattern recognition problem, looking at a 
  53. set of frames and deciding what pixels in the frame correspond to moving 
  54. objects---the sort of thing humans are very good at and computers very 
  55. bad at. For this reason, a complete MPEG compressor is complex and very 
  56. slow.
  57.  
  58. MPEG movies consist of three types of frames. I-frames are like JPEG 
  59. images and are compressed by themselves. P-frames are compressed based on 
  60. motion relative to a previous frame. B-frames are compressed based on 
  61. motion relative to both a previous frame AND a future frame. How do you 
  62. know what the future frame is? Well the MPEG data is not stored in the 
  63. same order as it is displayed. You have to decode future frames before 
  64. the B-frames on which they depend, then buffer the future frame 
  65. somewhere. This is why MPEG players need rather more memory than 
  66. QuickTime players.
  67.  
  68. As an example, here's a comment from my code.
  69. //About these three counters:
  70. //DecodedFrameNumber tells where we are in the file which we are currently
  71. //parsing, and is needed to find one's way around this file. It is incremented
  72. //every time a new frame is parsed.
  73. //DisplayedFrameNumber gives the number in temporal sequence of the frame that
  74. //is currently being shown on the screen. 
  75. //For I and P MPEGs these are the same,    but not for B MPEGs. For example a 
  76. //B MPEG may have the sequence:
  77. //    0    1    2    3    4    5    6    7    8    9    10    decodedFrameNumber
  78. //    I    P    B    B    I    B    B    P    B    B    I    frameType
  79. //    0     3    1    2 !    2    0    1    5    3    4 !    2    display number (within group)
  80. //     --------------|-----------------------|---- group boundaries
  81. //    1    4    2    3    7    5    6    10    8    9    ?    displayedFrameNumber
  82. //Note how the frames are clustered in groups, within which the Pict structure's
  83. //temporalReference field give the display number within that group. 
  84. //The displayedFrameNumber is basically a sum of these as one passes from group
  85. //to group, along with a condition of starting at one, rather than zero.
  86. //Now consider random access:
  87. //If we want to make a random access jump to a frame around displayed frame 5, 
  88. //we will be vectored to decodedFrameNumber 4, which will then be decoded, 
  89. //skipping past decodedFrameNumbers 5 and 6 (which depend on another frame in 
  90. //addition to decodedFrameNumber 4, and hence can't be displayed) to finally 
  91. //arrive at displaying decodedFrameNumber 4 as displayedFrameNumber 7. 
  92. //the variable decodedFrameNumberOfVisibleFrame keeps track of this fact that 
  93. //the displayedFrameNumber 7 actually represents decodedFrameNumber 4.
  94. //This information is necessary when stepping backwards through an MPEG. 
  95. //If we are at displayedFrameNumber 7 and step back, we will look back for I-frames
  96. //until we get to the I-frame at decodedFrameNumber==4. But this is the I-frame of
  97. //the image we are just displaying, so we actually need to then step back to an 
  98. //earlier I-frame. 
  99. //This complication is all necessary partially because of the way MPEG forward 
  100. //coding works, with the frame sequence on file not corresponding to the viewed
  101. //sequence, also partially because some B MPEGs do not have valid data for 
  102. //their Pict.temporalReference fields, thus one cannot rely on that field to be 
  103. //valid but one has to maintain a state machine as one parses through the file.
  104.  
  105. An MPEG movie can consist of only I-frames. This will be far from 
  106. optimally compressed, but is much easier to encode because the pattern 
  107. recognition is not needed. Such a movie is pretty much what you would get 
  108. if you made a QuickTime movie and used the JPEG codec as the compression 
  109. option. Because the I-frame movie is so much easier to calculate, it is 
  110. much more common. Sparkle checks if a movie uses only I-frames and if so 
  111. reduces its memory requirements since such movies do not need complex 
  112. buffering. In the PC world, many people talk about XING type MPEGs which 
  113. are pure I-frame MPEGs. These are produced by XING hardware on PCs and 
  114. played back using the XING MPEG player.
  115.  
  116. One problem with the MPEG standard is that many vendors seem to feel 
  117. which parts of it they support are optional. XING, for example, often 
  118. does not ends its MPEGs properly. It does not start frame numbering 
  119. properly, and does not correct frame numbering after MPEGs are edited.
  120. GC technologies produces MPEGs that have the frames essentially random 
  121. numbered, and has garbage frames at the start of its MPEGs.
  122. Wherever possible I have tried to adapt my code to common pathologies in 
  123. MPEG encoders. 
  124. I have also built in powerful yet computationally cheap 
  125. error-detection and recovery. For example a recent MPEG posted to usenet 
  126. drew widespread complaints because some of the uuencoded text was garbled 
  127. and the resultant MPEG crashed pretty much every decoder out there. But 
  128. Sparkle noticed the error and went on quite happily. Sparkle has also 
  129. proved quite robust in the face of MPEGs I have deliberately corrupted.
  130. If you come across any MPEG file that causes Sparkle to crash or produce 
  131. garbage, I WANT TO KNOW ABOUT IT. With a copy of the file, I can trace 
  132. through Sparkle, find just what causes the crash, and make Sparkle even 
  133. more robust.
  134.  
  135. For more details on MPEG, read the MPEG FAQ on USENET. It is posted once 
  136. a week to the picture groups and to news.answers.
  137.  
  138. ------------------------------------------------------------------------------
  139. ABOUT CONVERSION TO QUICKTIME
  140.  
  141. The following are notes I've made on conversion to QuickTime. I have  
  142. investigated this issue extensively, but not exhaustively. If someone has 
  143. comments on the subject---more extensive notes than I have, corrections, 
  144. whatever, please tell me.
  145.  
  146. All times I give are on my SE/30 with a 32-bit screen. People should 
  147. extrapolate to their machines---I guess LC IIs are about half as 
  148. fast and Centris/Quadras three to six times as fast.
  149.  
  150. The useful codecs are video, cinepak (used to be compact video) and jpeg. 
  151. JPEG compression at normal quality gives files of very good quality and not 
  152. much larger than pure I-frame MPEGs. A 120x160 image can play back at about 
  153. 4fps. Translated to an 040 and you get a useful frame rate. However JPEG 
  154. has a major problem in that when it decodes to a 32bit screen, it draws 
  155. directly to the screen, not to an offscreen Gworld unlike other codecs. 
  156. This produces movies with obvious tearing artifacts. When fast-dithering is 
  157. used to draw to other screen depths, it works fine. I don't understand why 
  158. this problem with 32 bit screens should be the case, but I have told Apple 
  159. about this problem and maybe it'll be fixed in a later release of 
  160. QuickTime. Meanwhile write to Apple and complain---they are holding back a 
  161. useful capability. 
  162.  
  163. With the video and cinepak compressors, it is very important to check the 
  164. key-frame rate checkbox. Key-frames are like MPEG I-frames. They are 
  165. compresed standalone and do not depend on other frames. The other frames 
  166. produced by the movie codecs depend on previous frames. Setting the 
  167. key-frame rate guarantees that at least that rate of key-frames (one 
  168. frame in used. for example) will be used. Checking the key-frame rate 
  169. checkbox allows the movie to use intra-frame compression (ie not just 
  170. key-frames) and gives movies half as small as they would otherwise be.
  171. The lower you set the key frame rate to (this means a larger number in 
  172. the QuickTime saving options dialog box) , the smaller you movie will be.
  173. For example a 72K MPEG (48 frames, 120x160, pure I-frame) became a 290K 
  174. movie without keyframes, a 160K movie with a key-frame rate of 1 in 8, 
  175. and a 138K movie with a key-frame rate of 1 in 96. 
  176. The price you pay for a low key-frame rate is that the movie has more 
  177. difficulty when playing backwards, or when randomly jumping around. I 
  178. don't find it a problem and usually use a key-frame rate of about 1 in 
  179. 100, but try for yourself to see what things are like.
  180. Video gives better quality results when a higher key-frame rate is used.
  181. Strangely cinepak appeared to give lower quality results (as well as a 
  182. larger movie) when more key-frame were used. 
  183. I'll have to investigate this further---I may have become confused when I 
  184. was making the measurements. Anyone want to confirm or deny this?
  185. (For comparison, this same movie became a 90K JPEG movie.)
  186.  
  187. I find video and cinepak give much the same file sizes at the same 
  188. (around normal) quality setting. The cinepak file is consistently a 
  189. little larger, but not enough to matter. The video file is consistently 
  190. lower quality for the same size as the cinepak file. However the video 
  191. low quality artifacts (blocks of solid color) I find less psychologically 
  192. irritating that the cinpeak low quality artifacts (general fuzzing of 
  193. borders like everything is drawn in crayon and watercolor). 
  194. However cinepak has the advantage of playing back much faster than video. 
  195. For a 120x160 image on my 32bit screen, I can get smooth playback with 
  196. cinepak at 24fps. Video can do smooth playback up to about 16 fps.
  197.  
  198. Fast dithering seems to be a good job for speed (at the cost of quality). 
  199. Unlike earlier versions of QuickTime, with 1.6.1 I found the same speed 
  200. of playback (ie same degree of skipping frames or not) at every screen 
  201. depth but 2 bit depth.
  202.  
  203. Cinepak can support a largish MPEG to QuickTime movie (352x240) at 6fps 
  204. on my mac, but no faster.
  205.  
  206. Compression using cinepak is SLOW SLOW SLOW. A 120x160 frame takes about 
  207. 10 seconds to compress. A 352x240 frame takes about a minute. In this 
  208. time your mac is stuck---it looks like it has crashed. Don't start saving 
  209. to cinepak QuickTime unless you are prepared to walk away from your mac 
  210. and not touch it until it's done.
  211. QuickTime 1.5 did not include anyway to do this compression in small 
  212. chunks so that it would run nicely in the background. I received word 
  213. today that QuickTime 1.6 does have this capability, so once I get the 
  214. relevant techincal documents and read them, I will add this ability.
  215.  
  216. See the WHY DOESN"T SPARKLE DO... section for more information about MPEG 
  217. frame rates and their relationship to QuickTime frame rates.
  218.  
  219. ------------------------------------------------------------------------------
  220. ABOUT QT VM
  221.  
  222. Recently an INIT called QT VM has been released. This attempts to speed up 
  223. the behavior of QuickTime under virtual memory. What it does is that when 
  224. an application makes a NewGWorld call, asking that the GWorld be placed in 
  225. TempMem, the call fails. The idea is that it is faster to force the memory 
  226. manager to move memory around the app's partition, purging and 
  227. consolidating, than it is to allocate the GWorld in new memory which may 
  228. force paging to disk. For many situations this is true, but not for 
  229. Sparkle. Sparkle tries to make itself use smaller partitions by creating 
  230. GWorlds in TempMem. With the QT VM INIT enabled, it can't do that and has 
  231. to create the GWorlds in its partition. This means that the partition size 
  232. has to be made larger occasionally.
  233.  
  234. ================================================================================
  235.  
  236. MISC SIZE AND TIMING NOTES
  237.  
  238. These are rough notes I take as I alter the code, partially out of interest 
  239. and partially to guide me in where I need to change things.
  240. They may be of interest to some of you.
  241. They are now timed on my new Q610 and the SE/30 is in the hands of my 
  242. little brother.
  243. Like the SE/30 timings given above, they may not be be consistent with 
  244. each other as they reflect the state of the code at different times. In 
  245. between I may change the code quite a bit---mainly of interest are the 
  246. differences within any group of results.
  247.  
  248. Timings for Erika1.MPG under Sparkle 2.0 on my Q610.
  249. This is a 41 frame pure I 120x160 frame MPEG.
  250.  
  251. These times are for a version of the code that does not call WNE while playing:
  252.  
  253. 1) Effect of the screen depth/dithering on times: (non-optimized code)
  254.     24-bit color:      8.2 s
  255.     16-bit color     9.2 s
  256.      8-bit color      10.1 s
  257.      8-bit grey         8.6 s
  258.      Conclusion:
  259.          probably worth adding hints to speed up some parts of the code to compensate
  260.          for the dithering times:
  261.          1) For  8 bit color use 4x4 IDCT.
  262.          2) For  8 bit grey, omit YUV->RGB conversion.
  263.          3) For 16 bit color, use a special YUV->RGB conversion.
  264.          
  265. 2) Effect of various TC6 optmizations:     (24-bit screen)
  266.     Defer and combine stack adjusts:        7.8 s
  267.     Suppress redundant loads:                7.7 s
  268.     Automatic register assignment:            7.6 s
  269.     Global:    
  270.         Induction variables:                7.4 s
  271.         Common sub-expression elimination:    7.3 s
  272.         Code motion:                        7.2 s
  273.         Register coloring:                    6.6 s
  274.  
  275. 3) Effects of various displayings:    (no optimizations)
  276.     No progress proc at all (implies NOTHING ever updated on screen):    6.7 s
  277.     Progress proc called but does nothing:                                6.8 s
  278.     Progress proc updates movie controller/text track only:                7.6 s
  279.     Progress proc updates only MPEG frames, not movie controller        7.3 s
  280.     Progress proc updates both:                                            8.1 s
  281.     Conclusion:
  282.         of the 8.1 s, 0.8 s=10% is used updating movie controller and
  283.                       0.5 s= 6% is used updating the MPEG frames.
  284.  
  285. 4) Effect of the time allowed a thread before it yields:
  286.     Yield time=6000 ticks (ie never yield)        8.0 s
  287.                 180 ticks                         8.1 s
  288.                  60 ticks                        8.2 s
  289.                  20 ticks                        8.6 s
  290.     One would rather have a 20 tick time than a 60 tick time for increased 
  291.     user interactivity, but the time cost is rather stiff.
  292.     However by implementing a new thread scheduler, I should be able to reduce
  293.     this cost somewhat.
  294.  
  295. 5) Effect of yield time in the background:
  296.     We convert Erika1.MooV to an I-frame MPG.
  297.     FG time (yield time of 30 ticks): 1min 12s
  298.     BG time (yield time of 10 ticks)  2min 30s
  299.     BG time (yield time of 30 ticks)  2min 04s
  300.     Conclusion:
  301.         The longer yield time is obviously better but makes things more choppy.
  302.         Best is probably to implement a timer keeping track of how fast we are 
  303.         getting background NULLs and increasing bgYieldTicks as we notice less
  304.         fg activity.
  305.  
  306. 6) Note: 
  307.     I have tried to put yield brackets around all the hotpoints of the code to 
  308.     make it run well in background. The main problem for now, that I need to work 
  309.     around (ProgressProcs ?) is when the new frame is requested for coding an MPEG
  310.     or QT movie from a QT document. The fiddling that goes on to obtain this frame
  311.     can be fairly substantial, taking as long as 70 or 80 ticks for a simple 
  312.     160x120 movie. My guess is that QT doesn't do very smart caching about 
  313.     non-synch frames and has to decompress a long sequence to get to these frames.
  314.     Anyways, because of this we're stuck with a basic jerkiness at that 
  315.     granularity for now.
  316.  
  317. 7) Effects of four different P algorithms.
  318.     We convert Erika1.MooV to four MPEGs, all using a PPPI pattern,
  319.     with an I-quantization of 8 and a P-quantization of 10.
  320.     Algorithm:                Time:            Size:
  321.     Logarithmic                1:45 min        53 953
  322.     Two level                2:45 min        54 328
  323.     Subsample                3:45 min        54 765
  324.     Exhaustive                5:55 min        54 677
  325.     There was no obvious difference in quality between these MPEGs (and they 
  326.     were all pretty lousy). Thus there seems no real advantage to using anything
  327.     but the fastest algorithm.
  328.  
  329. 8) Effects of P-quantization.
  330.     Evene with a P-quantization of 8, the above setup does not produce as good 
  331.     an image as a pure I sequence (although the file size of 62K is much smaller.)
  332.     This appears to be largely due to the successive dependencies caused by 
  333.     the three successive P frames. 
  334.     Is it better to reduce the number of Ps or lower the P-quantization?
  335.     Using same pattern but P-quantization of 4 gives a file size of 98K and 
  336.     a quality lower than the pure I-frames (though certainly better than what 
  337.     we had). Using a pattern of PPI and P-quantization of 8 gives a file size of 
  338.     71K and the same sort of quality.
  339.     
  340.     Using a PBBIBB pattern and all quantizations as 8 gives a size of 60K and 
  341.     the same sort of quality. 
  342.     
  343.     Conclusions:
  344.     1) I need to use a higher quality source of images to investigate these 
  345.     affects. 
  346.     2) I think the P and B pattern matching criteria may be a bit dodgy, or maybe
  347.     some part of my code has problems with half-pixels or such.
  348.  
  349. 9) Effect of buffer size.
  350.     I played a 750K MPEG of 150 frames. With a buffer size of two frames, it took
  351.     36s. With a buffer size of 200 frames (ie entire movie) it took 33s. Thus
  352.     the larger buffer buys about 10% speed. 
  353.     So maybe, when time, create massive buffers which are in some way shrinkable.
  354. ----------------------------------------------------------------------------------
  355.  
  356. Sizings for Erika1.MPG under Sparkle 2.0
  357.  
  358. 1) Using only I-frame encoding with varying I-quantization:
  359.     I-quantization            size in bytes
  360.             1                    237 307
  361.             2                    179 960
  362.             4                    132 916
  363.             8                     92 210
  364.            16                     66 821
  365.            24                     42 658
  366. //These two values are bogus, now that I've cleaned up the MPEG generating 
  367. //code.
  368. //           32                      37 094
  369. //           64                    25 955
  370.     DC terms only                 21 695
  371.     
  372.     Notes:
  373.     Ñ These sizes are probably slightly larger than necessary as at present I do not
  374.       pad the excess pixels where frame size is smaller than the frame size in 
  375.       macroblocks, thus the DCT is encoding crud at those borders. By padding those 
  376.       to DC we'll get a small shrinkage in size.
  377.     ! This was fixed in version 2.01. The shrinkage was way more than I 
  378.       expected, of the order of 15%.
  379.     Ñ With this set of images (which were pretty lousy to begin with) a quantization 
  380.       level of 8 produced acceptable images, while a level of 16 produced 
  381.       unnacceptable quality.
  382. ================================================================================
  383.  
  384. ABOUT THE THREAD USAGE
  385.  
  386. I have nothing special to say about using threads except that I recommend 
  387. all serious Mac coders read the Apple documentation and use them. They 
  388. make life so much easier. The 1.x code was full of the most ghastly and 
  389. convoluted logic to enable breaking out of the MPEG decoder into the main 
  390. event loop and back again. However the 2.x code for encoding is ridiculously
  391. simple. We simply have the encoder, written (like a UNIX process or such) 
  392. as on long loop, then in the loop at appropriate points we make Yield() 
  393. calls.
  394.  
  395. The one thing that one has to be careful of is using exception handling in 
  396. the TCL. Because this is based on application wide globals, dreadful 
  397. things can happen in your thread when an exception occurs, a string of 
  398. CATCH handlers is followed up the stack, and at some point you leave the 
  399. stack of the thread and enter the "stack of the application". My solution 
  400. to this was to use custom thread context switchers which, on every context 
  401. switch, swap the appropriate exception handling globals.
  402. The custom context switchers also become a good place for updating the 
  403. timings of each thread and setting when it will next yield.
  404.  
  405. Another good idea is to encapsulate the thread in an object, then call 
  406. that object to yield instead of calling a Thread Manager yield directly. 
  407. The advantage of this is that the thread object can then see if the thread 
  408. manager is installed
  409. ---if so it yields,
  410. ---if not it can test for command-. and abort, or simly return control.
  411. This makes the code inside each inner loop (MPEG encoding or decoding or 
  412. QT encoding) much cleaner---simply yields with no tests for this or that 
  413. special situation.
  414.  
  415. At present I'm only using cooperative threads. It's not clear to me that 
  416. switching to pre-emptive threads is a useful excercise. One problem is, of 
  417. course, that pre-emptive threads make life rather trickier and coding more 
  418. complex. More to the point, pre-emptive threads only get half the CPU 
  419. time, while the WaitNextEvent() loop gets the other half. So by switching 
  420. to them I'd get lose half my speed, and not gain much. I might gain 
  421. slightly smoother user event support, especially in the background, but 
  422. that's not that bad right now and will improve when I install a custom 
  423. thread scheduler in place of the hokey quick kludge I'm using right now.
  424. If anyone out there has worked with pre-emptive threads and has opinions 
  425. on them one way or the other, please let me know.
  426.  
  427. A second major change in the 2.x code is I have now structured things 
  428. around a model of video source objects and video encoder objects, with any 
  429. video source able to be linked to any video encoder.
  430. This makes for very orthogonal extensible code.
  431. The natural extension of this is now to define more video sources. In 
  432. particular as soon as I can I hope to get to work on morphing routines, 
  433. with output that can be played to screen or saved in whatever video 
  434. formats I'm supporting by that stage. I have some ideas for morphing 
  435. algorithms, but if anyone can send me code, or tell me whence I can ftp it 
  436. (yes this usage of whence is correct) I'll obviously be able to get going 
  437. faster. Along the same lines, anyone know where I can get AVI source, or 
  438. the AVI specs so I can add AVI support?
  439.  
  440. UPDATE FOR 2.1
  441.  
  442. The connection between threads is now based on a message queue associated 
  443. with each thread. When a message is passed to a thread it is enqueued. 
  444. If a thread is busy (ie playing or saving to some format) and asks for 
  445. messages when there are none it is given a NULL message which it uses to 
  446. perform idle processing, otherwise it is put to sleep. Obviously this 
  447. mechanism looks very like the Process Manager's behavior. 
  448. Two consequences emerge from this.
  449. The first is that I can now, in the main event loop, peek for events and 
  450. if there are no events in my main event queue, return immediately. This 
  451. allows me to avoid the overhead of (very expensive) main event loop while 
  452. maintaining high interactivity. The cost of high interactivity is thus 
  453. reduced from about 12% of play time to about 1%.
  454. The second is that it makes it much easier to glue the user interface to a 
  455. different MPEG encoder or decoder (eg dedicated hardware) because the 
  456. connection between the user interface and the threads doing the work is 
  457. asynchronous.
  458. ================================================================================
  459.  
  460. ABOUT THE RESOURCES.
  461.  
  462. The default for the flags for all resources is purgable. 
  463. However there are some exceptions.
  464. Ñ╩DLOGs and DITLs that will be opened by the TCL needed to be set nonpurgable
  465.     because of a bug in the TCL. I have altered CDLOGDialog to fix this and
  466.     these resources can now be purgable.
  467. Ñ The DLOGs and DITLs used by CustomGetFile() and CustomPutFile() appear to 
  468.     need to be non-purgable even though IM says they can be purgable. If they 
  469.     are set    purgable the MemHell INIT registers errors during CustomGetFile() 
  470.     and CustomPutFile().
  471. Ñ╩Menus may not be purgable because the menu manager does not allow that. 
  472.     Given this, one might as well make them preload and locked.
  473.     Likewise for the MBAR.
  474. Ñ╩The DaTa resources, used to construct decoding tables, are mostly preload 
  475.     locked and treated just liked const global data. However there are a few 
  476.     small tables for which it is advantageous to load these into genuine global
  477.     arrays. For that case, the resources are marked purgable.
  478. Ñ Marking resources Protected does not ever seem to be useful.
  479.  
  480. Ñ If a dialog/alert makes uses of an ictb to change the font of text items, 
  481.     the associated dctb or actb must also be present or else nothing will 
  482.     happen.
  483. Ñ╩Note that some of the dctb/itcb resources may appear redundant. However they 
  484.     prove to be necessary in unexpected ways. For example if they are not 
  485.     present for the CustomPutFile() DLOG, the dialog box drawn on screen will 
  486.     use dotted grey pattern to draw the items in the scrolling list of files,
  487.     rather than using a nice grey color. 
  488. ================================================================================
  489.  
  490. TIMING FOR WAITNEXTEVENT() AND FRIENDS
  491.  
  492. I wrote a simple loop that timed 200 passes each of these calls, and 
  493. recorded the times. For my outer loop over events, I want to know the cheapest 
  494. way of ascertaining whether I should get an event or not:
  495. For the last item, we timed 200 loops over the TCL core event routine,
  496. CApplication::Process1Event(), with nothing happening.
  497.  
  498. Time (in ticks) for 200 passes of:
  499.     EventAvail()           30
  500.     GetNextEvent          62
  501.     WaitNextEvent0          93        (WNE with mouseRegion==NULL and sleep==0)
  502.     WaitNextEvent1         493        (WNE with mouseRegion==NULL and sleep==1)
  503.     Loops over TCL Idle 1027
  504. ================================================================================
  505.  
  506. ACCURATE TIMINGS FOR SPARKLE.
  507.  
  508. These are timings taken for Sparkle 2.1 on my Q610 with 24bit color. The idea was
  509. to accurately time what were the hot spots in MPEG playback.
  510. All times reported are in ticks (60th of a second) and reflect the second time 
  511. the operation was performed. Very consistently it was found that the first time 
  512. an operation was performed took 11 ticks longer than subsequent times, presumably
  513. reflecting loading in purgable resources and such initialization.
  514.  
  515. Times to play Erika1.mpg. All times reflect playback time for 40 frames.
  516. The first time is split into times with debugger and without. 
  517. Once the program never calls WNE the debugger time becomes the same as the app time.
  518.  
  519. Standard parameters, thread time quantum=20 ticks, 
  520.   delay before yielding to other apps via WNE was infinity
  521.     When debugging time was         433 ticks         5.5 fps
  522.     As an application               402 ticks        6.0 fps
  523.     
  524. If we set an infinite yield time    
  525.                                     399 ticks        6.0 fps
  526. // All subsequent times use an infinite yield time.
  527. If we don't use a progress proc     
  528.                                     370 ticks
  529.  
  530. If we use a progress proc but it doesn't update the screen
  531.                                     373 ticks
  532.                 
  533. If we update the screen but don't use a movieController
  534.                                     332 ticks
  535.  
  536. //All subsequent times use an infinite yield time and no movie controller.
  537. If we omit YUV to RGB conversion
  538.                                     225 ticks
  539.                                     
  540. If we use only a DC term IDCT                                     
  541.                                     229 ticks
  542.  
  543. If we don't even call IDCT
  544.                                     226 ticks
  545.  
  546. If we don't call ReconstructIBlock()
  547.                                     295 ticks
  548.                                     
  549. If we use a 4x4 pseudo IDCT (very rough---can be written to be faster)
  550.                                     320 ticks
  551.  
  552. If we use QUALITY_STANDARD not QUALITY_HIGH
  553.                                     324 ticks
  554.                                     
  555. If we use QUALITY_LOW not QUALITY_HIGH
  556.                                     294 ticks
  557.  
  558.                                     
  559. From this we conclude that for 40 frames:
  560. WNE/thread yield overhead=3 to 4 ticks in an app, but about 35 ticks when debugging.
  561.     (This is nice---it means my scheme for drastically cutting WNE time works!)
  562. Progress proc function call overhead= 3 ticks.
  563.  
  564. CopyBits to 24bit screen= 25 ticks
  565. MovieController overhead= 40 ticks
  566. YUV to RGB                 =105 ticks
  567. IDCT                    =100 ticks
  568. Reconstruct I blocks    = 30 ticks (does cropping of the results)
  569. So Huffman                = 95 ticks
  570.  
  571. From these results we can expect to shave 15 ticks off the IDCT by using 4x4.
  572. We can shave off 30 ticks by using QUALITY_LOW.
  573. We can probably cut YUV to RGB in two or more by using the upper 5 bits of each 
  574. of YUV into a table.
  575.  
  576.  
  577. Some further results are:
  578.  
  579. Suppose we simply run the movie controller/text track and don't ever call 
  580. the MPEG code. This takes about 55 ticks.
  581. Suppose we hide the movie controller:    422 ticks (cf 433 ticks)
  582.                     text track        :    418 ticks
  583.                     both            :    408 ticks